<template>
  <div class="v-app-container" :class="{'v-is-electron': $global?.isElectron}">
    <router-view />
    <Portal />
  </div>
</template>
<script lang="ts" setup>
import '@/assets/css/app.scss';
import Portal from './common/Portal.vue';
import { ipcRenderer } from "electron";
import { IpcMsg, MessageChannelType } from './engine/types/Ipc';
import { onMounted, onUnmounted } from 'vue';
import { simHttp } from "@/util/httpClient/SimulationHttpClient";
import { API } from '@/constants/api';
import { controller } from '@/engine/Controller';
import { SimSubwinMsgToSimWin, SimSubwinMsgToSimWinData, SubWinName } from './engine/types/Win';

const dealLangChange = async(event: Event, data: string) => {
  controller.locale.setLocal(data);
  let lang = data === 'zhCn' ? 'zh' : 'en'; // fe and be's key no-correspond
  await simHttp
    .get({
      api: "/console/changeLocale",
      data: { language: lang }
    });
};
const handleMsgChannel = async (event: Event, obj: any) => {
  if (!obj) return;
  switch (obj.type) {
    case 'SimulationMdesignConfigChange':
      controller.simulationConfig = { ...obj.data };
      break;
    case 'SimVariableValue':
      controller.simStatus.getSimTips(obj.data);
      break;
    case 'SimCreatedInteraction':
      await simHttp
        .get({
          api: API.SIM_CREATE_INTERACTION,
          data: { simulationSessionId: controller.sessionId, modelId: obj.data.modelId, diagramModelId: obj.data.diagramModelId }
        });
      break;
    default:
  }
};

const handleSimSubwinMsgToSimWin = async (event: Event, obj: SimSubwinMsgToSimWinData) => {
  if (!obj) return;
  switch (obj.type) {
    case SimSubwinMsgToSimWin.ExportActPinDataClose:
      controller.winService.subWins = controller.winService.subWins.filter(w => w.name !== SubWinName.ExportActPinData);
      break;
    case SimSubwinMsgToSimWin.SubMounted:
      controller.winService.mountedSubWins++;
      break;
    case SimSubwinMsgToSimWin.TimeLineKeepOpen:
      controller.winService.closeExceptWins.push(SubWinName.Timeline);
      break;
    case SimSubwinMsgToSimWin.TimeSeryKeepOpen:
      controller.winService.closeExceptWins.push(SubWinName.Charts);
      break;
    default:
      break;
  }
};

onMounted(() => {
  controller.ipcService.projectClose();
  ipcRenderer.on(IpcMsg.LocaleChange, dealLangChange);
  ipcRenderer.on(IpcMsg.MessageChannel, handleMsgChannel);
  ipcRenderer.on(IpcMsg.SimSubwinMsgToSimWin, handleSimSubwinMsgToSimWin);
});
onUnmounted(async () => {
  ipcRenderer.off(IpcMsg.LocaleChange, dealLangChange);
  ipcRenderer.off(IpcMsg.MessageChannel, handleMsgChannel);
  ipcRenderer.off(IpcMsg.SimSubwinMsgToSimWin, handleSimSubwinMsgToSimWin);
});
</script>
<style lang="scss">
.v-app-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;

}
._main-content {
  flex: 1;
  width: 100%;
  height: calc(100% - 97px);
}
</style>
